package com.lanhai.ads.service;

import com.lanhai.ads.dao.*;
import com.lanhai.ads.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.lang.model.util.SimpleElementVisitor6;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ReportService {
    @Autowired
    RptTransferDao rptTransferDao;
    @Autowired
    RptTransferDayDao rptTransferDayDao;
    @Autowired
    RptTransferMonthDao rptTransferMonthDao;
    @Autowired
    RptTransferStepDao rptTransferStepDao;
    @Autowired
    RptStateTimeDao rptStateTimeDao;

    /**
     * 查询指定日班别的各AGV接料合计数据
     * @param day
     * @param shift
     * @return
     * @throws ParseException
     */
    public List<RptTransfer> listRptTransfer(String day, String shift) throws ParseException {
        RptTransferExample example = new RptTransferExample();
        RptTransferExample.Criteria criteria = example.createCriteria();
        criteria.andWorkShiftEqualTo(shift);
        SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd" );
        Date d = sdf.parse(day);
        criteria.andWorkdayEqualTo(d);
        return rptTransferDao.selectByExample(example);
    }

    /**
     * 查询指定日期范围的各班次接料合计
     * @param begin
     * @param end
     * @return
     * @throws ParseException
     */
    public List<RptTransferDay> listRptTransferDay(String begin, String end) throws ParseException {
        RptTransferDayExample example = new RptTransferDayExample();
        RptTransferDayExample.Criteria criteria = example.createCriteria();
//        criteria.andWorkdayGreaterThanOrEqualTo(SimpleDateFormat.getDateInstance().parse(begin));
//        criteria.andWorkdayLessThanOrEqualTo(SimpleDateFormat.getDateInstance().parse(end));
        SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd" );
        Date d1 = sdf.parse(begin);
        Date d2= sdf.parse(end);
        criteria.andWorkdayBetween(d1, d2);
        return rptTransferDayDao.selectByExample(example);
    }

    /**
     * 月度搬运量统计（指定年）
     * @param year
     * @param begin 开始月份
     * @param end 结束月份
     * @return
     */
    public List<RptTransferMonth> listRptTransferMonth(Integer year, Integer begin, Integer end) {
        RptTransferMonthExample example = new RptTransferMonthExample();
        RptTransferMonthExample.Criteria criteria = example.createCriteria();
        criteria.andYearEqualTo(year);
        if(begin!=null && end!=null){
            criteria.andMonthBetween(begin, end);
        }else if(begin !=null){
            criteria.andMonthGreaterThanOrEqualTo(begin);
        }else if(end!=null){
            criteria.andMonthLessThanOrEqualTo(end);
        }
        return rptTransferMonthDao.selectByExample(example);
    }

    /**
     * 查询任意时间段的月度搬运量统计(似乎不需要）
     * @param begin
     * @param end
     * @return
     */
    @Deprecated
    public List<RptTransferMonth> listRptTransferMonth(Date begin, Date end) {
        //select * from rpt_transfer_month where year>y1 and month>m1 and year<y2 and month<m2
        Calendar d1 = Calendar.getInstance();
        Calendar d2 = Calendar.getInstance();
        d1.setTime(begin);
        d2.setTime(end);
        return null;
    }

    public List<RptTransferStep> listRptTransferStep(String day, String workshift) throws ParseException {
        RptTransferStepExample example = new RptTransferStepExample();
        RptTransferStepExample.Criteria criteria = example.createCriteria();
        SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd" );
        Date d = sdf.parse(day);
        criteria.andWorkdayEqualTo(d); //2021-09-09
        criteria.andWorkShiftEqualTo(workshift);
        return rptTransferStepDao.selectByExample(example);
    }

    public List<RptStateTime> listRptStateTime(String day, String workshift) throws ParseException {
        RptStateTimeExample example = new RptStateTimeExample();
        RptStateTimeExample.Criteria criteria = example.createCriteria();
        SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd" );
        Date d = sdf.parse(day);
        criteria.andWorkdayEqualTo(d);
        criteria.andWorkShiftEqualTo(workshift);
        example.setOrderByClause("agv_id");
        return rptStateTimeDao.selectByExample(example);
    }
}
